//
//  collectionVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class collectionVC: UIViewController, UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var article: UILabel!//文章
    @IBOutlet weak var company: UILabel!//公司
    @IBOutlet weak var brand: UILabel!//品牌
    var modelList: mySelfCollectionInfo?{
        didSet{
            if self.modelList != nil{
                self.initRightItem()
                self.tableView.reloadData()
                self.showNumber()
                
            }
           
        }
    }
    var msg: regiestInfo?{
        didSet{
            self.tableView.reloadData()
        }
    }
    var newmodel: mySelfCollectionNewModelInfo?{
        didSet{
            self.showNumber()
            self.tableView.reloadData()
            
        }
    }
    var type: String = "1"
    // 充当tableView的数据源
    
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.tableView.delegate = self
        self.tableView.tableHeaderView = headerView
        self.tableView.dataSource = self
       self.getMyCollectionNewModelInfo(self.type)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        self.tabBarController?.tabBar.hidden = true
    }
    override func viewWillDisappear(animated: Bool) {
        SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    func showNumber() -> Void {
        self.article.text = self.newmodel?.wznum
        self.brand.text = self.newmodel?.ppnum
        self.company.text = self.newmodel?.gsnum
    }
    //MARK: initRightItem
    func initRightItem(){
        let shareBtn = UIButton(frame: CGRectMake(0, 0, 25, 25))
        shareBtn.setBackgroundImage(UIImage(named: "right_item")!, forState: UIControlState.Normal)
        shareBtn.addTarget(self, action: #selector(self.shareAction), forControlEvents:  UIControlEvents.TouchUpInside)
        let share = UIBarButtonItem(customView: shareBtn)
        self.navigationItem.rightBarButtonItem = share
    }
    
    func shareAction(){
        self.tableView.editing = !self.tableView.editing
    }

    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.type == "1"{
            if self.newmodel?.sc!.count == nil{
                return 0
            }else{
                return (self.newmodel?.sc!.count)!
            }
        }else if self.modelList?.sc!.count == nil{
            return 0
        }else{
            return (self.modelList?.sc?.count)!
        }
        
       
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if self.type == "1"{
            let newsModel = self.newmodel?.sc![indexPath.row]
            return newsModel!.cellType!.cellHeight()
        }
        if self.type == "2"{//明星企业新闻列表
            return 115
            
        }else{//专业产品新闻列表
            let model = self.modelList!.sc![indexPath.row]
            if model.imgType != 0 && model.imgType > 0{
                return 210
            }else if model.imgextra != nil{
                return 130
            }else{
                return 95
            }
            
        }

    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if self.type == "1"{

            let model = self.newmodel?.sc![indexPath.row]
            
            let cell = tableView.dequeueReusableCellWithIdentifier(model!.cellType.rawValue, forIndexPath: indexPath) as! NewsCell
           
            cell.newsModel = model
            
            return cell
            
        }else if self.type == "2"{
            let cell = tableView.dequeueReusableCellWithIdentifier("specialChannelCell", forIndexPath: indexPath) as! specialChannelCell
            
            let model = self.modelList?.sc![indexPath.row]
            
            cell.iv.sd_setImageWithURL(NSURL(string: model!.timg), placeholderImage: UIImage(named: "placeholder"))
            cell.addressLbl.text = String(format: "%@%@%@",model!.unitaddress ,model!.unitarea ,model!.unittype)
            cell.id = model!.id
            cell.intruductLbl.text = model!.introduction
            cell.titleLbl.text = model!.unitname
            return cell

        }else{
            let model = self.modelList?.sc![indexPath.row]
            if model!.imgType != 0 && model!.imgType > 0{
                let cell = tableView.dequeueReusableCellWithIdentifier("specialBigPictureCell", forIndexPath: indexPath) as! specialBigPictureCell//大图显示
                cell.titleLbl.text = model!.name
                cell.iv.sd_setImageWithURL(NSURL(string: model!.imgsrc), placeholderImage: UIImage(named: "placeholder"))
                cell.levelLbl.text = model?.reputation
                return cell
                
            }
            if  model!.imgextra != nil{
                let cell = tableView.dequeueReusableCellWithIdentifier("specialThreePictureCell", forIndexPath: indexPath) as! specialThreePictureCell//三张图显示
                cell.titleLbl.text = model!.name
                cell.iv1.sd_setImageWithURL(NSURL(string: model!.imgextra![0]), placeholderImage: UIImage(named: "placeholder"))
                cell.iv2.sd_setImageWithURL(NSURL(string: model!.imgextra![1]), placeholderImage: UIImage(named: "placeholder"))
                cell.iv3.sd_setImageWithURL(NSURL(string: (model!.imgextra![2])), placeholderImage: UIImage(named: "placeholder"))
                cell.levelLbl.text = model?.reputation
                return cell
                
            }else{
                let cell = tableView.dequeueReusableCellWithIdentifier("specialNormalCell", forIndexPath: indexPath) as! specialNormalCell//普通显示
                cell.titleLbl.text = model!.name
                cell.iv.sd_setImageWithURL(NSURL(string: model!.imgsrc), placeholderImage: UIImage(named: "placeholder"))
                cell.levelLbl.text = model?.reputation
                return cell
            }

            
        }
        
       
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if self.type == "1"{

            let sb = UIStoryboard(name: "NewsList", bundle: NSBundle.mainBundle())
            let vc = sb.instantiateViewControllerWithIdentifier("DetaillNewsController") as! DetaillNewsController
            vc.newsModel = self.newmodel?.sc![indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)

        }else if self.type == "2"{
            let sb = UIStoryboard(name: "NewsList", bundle: NSBundle.mainBundle())
            let vc = sb.instantiateViewControllerWithIdentifier("SpecialStarDetailVC") as! SpecialStarDetailVC
            vc.id = (self.modelList?.sc![indexPath.row].id)!
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            let sb = UIStoryboard(name: "NewsList", bundle: NSBundle.mainBundle())
            let vc = sb.instantiateViewControllerWithIdentifier("SpectailDetail") as! SpectailDetail
            vc.id = (self.modelList?.sc![indexPath.row].id)!
            self.navigationController?.pushViewController(vc, animated: true)
            
        }
        
    }
    
    //删除某行cell
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
                   forRowAtIndexPath indexPath: NSIndexPath){
        if(editingStyle == UITableViewCellEditingStyle.Delete) {
            
            if self.type == "1"{
            let model = self.newmodel?.sc![indexPath.row]
                self.delegateMyCollection((model?.NewsModelid)!, type: String(1))
            
            }
            else{
                let model = self.modelList?.sc![indexPath.row]
               self.delegateMyCollection((model?.id)!, type: (self.type))
                
            }
            
        }
        
    }
    //是否打开可以编辑
    func tableView(tableView: UITableView,
                   canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        return true
        
    }
    //是否可以滑动删除
    func tableView(tableView: UITableView,
                   canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        return true
        
    }
    //编辑样式
    func tableView(tableView: UITableView,
                   editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle{
        
        return .Delete
        
    }

   
    //MARK:target
    @IBAction func articleBtnClicked(sender: UIButton){
        self.type = "1"
       self.getMyCollectionNewModelInfo(self.type)
    }
    @IBAction func brandBtnClicked(sender: UIButton){
        self.type = "3"
        getMyCollectionInfo(self.type)
    }
    @IBAction func companyBtnClicked(sender: UIButton){
        self.type = "2"
        getMyCollectionInfo(self.type)
    }
    
    //MARK:NETWORKING
    func getMyCollectionInfo(num: String) -> Void {
        DataTool.myCollectionInfo(num){ (modelList) -> Void in
            guard modelList != nil else{
                return
            }
           
            self.modelList = modelList

        }
        
    }
    func getMyCollectionNewModelInfo(num: String) ->Void {
        DataTool.myCollectionNewModelInfo(num){ (modelList) -> Void in
            guard modelList != nil else{
                return
            }
            self.newmodel = modelList
            
            
        }

    }
    //删除数据
   func delegateMyCollection(id: String, type: String){
        DataTool.delegateMyCollection(id, type: type, completionHandler:{ (newModel) in
                guard let newDataes = newModel else {
                    return
            }
            
                print(newDataes)
            self.tableView.editing = false
            self.msg = newDataes
            if self.type == "1"{
                self.getMyCollectionNewModelInfo(self.type)
            }else{
                
                self.getMyCollectionInfo(self.type)
            }
            
            
            SVProgressHUD.showSuccessWithStatus(self.msg!.msg)
              
                        
        })

    }
    
}

